﻿using System;
using System.Diagnostics;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using HslCommunication.BasicFramework;
using HslCommunication.Core.IMessage;
using HslCommunication.LogNet;
using HslCommunication.ModBus;

namespace HslCommunication.DCS
{
	/// <summary>
	/// 南京自动化研究所的DCS系统，基于modbus实现，但是不是标准的实现
	/// </summary>
	// Token: 0x0200010A RID: 266
	public class DcsNanJingAuto : ModbusTcpNet
	{
		/// <summary>
		/// 实例化一个默认的对象<br />
		/// Instantiate a default object
		/// </summary>
		// Token: 0x06001791 RID: 6033 RVA: 0x0007825D File Offset: 0x0007645D
		public DcsNanJingAuto()
		{
		}

		/// <inheritdoc />
		// Token: 0x06001792 RID: 6034 RVA: 0x0007827F File Offset: 0x0007647F
		public DcsNanJingAuto(string ipAddress, int port = 502, byte station = 1) : base(ipAddress, port, station)
		{
		}

		/// <inheritdoc />
		// Token: 0x06001793 RID: 6035 RVA: 0x000782A4 File Offset: 0x000764A4
		protected override INetMessage GetNewNetMessage()
		{
			return new DcsNanJingAutoMessage();
		}

		/// <inheritdoc />
		// Token: 0x06001794 RID: 6036 RVA: 0x000782AC File Offset: 0x000764AC
		protected override OperateResult InitializationOnConnect(Socket socket)
		{
			base.MessageId.ResetCurrentValue(0L);
			this.headCommand[6] = base.Station;
			OperateResult operateResult = base.Send(socket, this.headCommand);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.Receive(socket, -1, 3000, null);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = (this.CheckResponseStatus(operateResult2.Content) ? base.InitializationOnConnect(socket) : new OperateResult("Check Status Response failed: " + operateResult2.Content.ToHexString(' ')));
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06001795 RID: 6037 RVA: 0x00078350 File Offset: 0x00076550
		[DebuggerStepThrough]
		protected override Task<OperateResult> InitializationOnConnectAsync(Socket socket)
		{
			DcsNanJingAuto.<InitializationOnConnectAsync>d__4 <InitializationOnConnectAsync>d__ = new DcsNanJingAuto.<InitializationOnConnectAsync>d__4();
			<InitializationOnConnectAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<InitializationOnConnectAsync>d__.<>4__this = this;
			<InitializationOnConnectAsync>d__.socket = socket;
			<InitializationOnConnectAsync>d__.<>1__state = -1;
			<InitializationOnConnectAsync>d__.<>t__builder.Start<DcsNanJingAuto.<InitializationOnConnectAsync>d__4>(ref <InitializationOnConnectAsync>d__);
			return <InitializationOnConnectAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06001796 RID: 6038 RVA: 0x0007839C File Offset: 0x0007659C
		public override OperateResult<byte[]> ReadFromCoreServer(Socket socket, byte[] send, bool hasResponseData = true, bool usePackHeader = true)
		{
			ILogNet logNet = base.LogNet;
			if (logNet != null)
			{
				logNet.WriteDebug(this.ToString(), StringResources.Language.Send + " : " + (this.LogMsgFormatBinary ? send.ToHexString(' ') : Encoding.ASCII.GetString(send)));
			}
			INetMessage newNetMessage = this.GetNewNetMessage();
			bool flag = newNetMessage != null;
			if (flag)
			{
				newNetMessage.SendBytes = send;
			}
			OperateResult operateResult = base.Send(socket, send);
			bool flag2 = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag2)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				bool flag3 = this.receiveTimeOut < 0;
				if (flag3)
				{
					result = OperateResult.CreateSuccessResult<byte[]>(new byte[0]);
				}
				else
				{
					bool flag4 = !hasResponseData;
					if (flag4)
					{
						result = OperateResult.CreateSuccessResult<byte[]>(new byte[0]);
					}
					else
					{
						bool flag5 = base.SleepTime > 0;
						if (flag5)
						{
							Thread.Sleep(base.SleepTime);
						}
						OperateResult<byte[]> operateResult2 = base.ReceiveByMessage(socket, this.receiveTimeOut, newNetMessage, null);
						bool flag6 = !operateResult2.IsSuccess;
						if (flag6)
						{
							result = operateResult2;
						}
						else
						{
							ILogNet logNet2 = base.LogNet;
							if (logNet2 != null)
							{
								logNet2.WriteDebug(this.ToString(), StringResources.Language.Receive + " : " + (this.LogMsgFormatBinary ? operateResult2.Content.ToHexString(' ') : Encoding.ASCII.GetString(operateResult2.Content)));
							}
							bool flag7 = operateResult2.Content.Length == 6 && this.CheckResponseStatus(operateResult2.Content);
							if (flag7)
							{
								operateResult2 = base.ReceiveByMessage(socket, this.receiveTimeOut, newNetMessage, null);
							}
							bool flag8 = newNetMessage != null && !newNetMessage.CheckHeadBytesLegal(base.Token.ToByteArray());
							if (flag8)
							{
								if (socket != null)
								{
									socket.Close();
								}
								result = new OperateResult<byte[]>(string.Concat(new string[]
								{
									StringResources.Language.CommandHeadCodeCheckFailed,
									Environment.NewLine,
									StringResources.Language.Send,
									": ",
									SoftBasic.ByteToHexString(send, ' '),
									Environment.NewLine,
									StringResources.Language.Receive,
									": ",
									SoftBasic.ByteToHexString(operateResult2.Content, ' ')
								}));
							}
							else
							{
								result = OperateResult.CreateSuccessResult<byte[]>(operateResult2.Content);
							}
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06001797 RID: 6039 RVA: 0x000785F0 File Offset: 0x000767F0
		[DebuggerStepThrough]
		public override Task<OperateResult<byte[]>> ReadFromCoreServerAsync(Socket socket, byte[] send, bool hasResponseData = true, bool usePackHeader = true)
		{
			DcsNanJingAuto.<ReadFromCoreServerAsync>d__6 <ReadFromCoreServerAsync>d__ = new DcsNanJingAuto.<ReadFromCoreServerAsync>d__6();
			<ReadFromCoreServerAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadFromCoreServerAsync>d__.<>4__this = this;
			<ReadFromCoreServerAsync>d__.socket = socket;
			<ReadFromCoreServerAsync>d__.send = send;
			<ReadFromCoreServerAsync>d__.hasResponseData = hasResponseData;
			<ReadFromCoreServerAsync>d__.usePackHeader = usePackHeader;
			<ReadFromCoreServerAsync>d__.<>1__state = -1;
			<ReadFromCoreServerAsync>d__.<>t__builder.Start<DcsNanJingAuto.<ReadFromCoreServerAsync>d__6>(ref <ReadFromCoreServerAsync>d__);
			return <ReadFromCoreServerAsync>d__.<>t__builder.Task;
		}

		// Token: 0x06001798 RID: 6040 RVA: 0x00078654 File Offset: 0x00076854
		private bool CheckResponseStatus(byte[] content)
		{
			bool flag = content.Length < 6;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				for (int i = content.Length - 4; i < content.Length; i++)
				{
					bool flag2 = content[i] > 0;
					if (flag2)
					{
						return false;
					}
				}
				result = true;
			}
			return result;
		}

		// Token: 0x04000535 RID: 1333
		private byte[] headCommand = new byte[]
		{
			0,
			0,
			0,
			0,
			0,
			6,
			1,
			3,
			0,
			0,
			0,
			1
		};
	}
}
